package com.zl.rbac.web.interceptor;

import com.zl.rbac.entity.User;
import com.zl.rbac.util.UserContext;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 检查是否登录的拦截器
 * @ClassName: CheckLoginInterceptor
 * @author: suke
 * @since: 2021/1/13 16:12
 */
@Component
public class CheckLoginInterceptor  implements HandlerInterceptor{
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //methodHandler
        //判断session使用有user对象
        if(UserContext.getCurrent() == null){  //没有登录
            //public java.lang.String com.zl.rbac.web.controller.UserController.list(java.lang.Integer,org.springframework.ui.Model) throws java.lang.Exception
            System.out.println(handler);
            //重定向到登录页面
            response.sendRedirect("/login.html");
            return false;
        }
        return true;
    }
}
